package ru.spb.osll.myfit.server;

import java.io.File;
import java.io.FilenameFilter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import ru.spb.osll.myfit.common.TextParsingTools;

public class Configurator {
  private Logger log = Logger.getLogger(getClass());
  private static final String JAR_FILE = ".jar";
	
  /**
   * Path to all jars
   */
  private String m_jars;
  /**
   * Path to fixtures jars
   */
  private String m_fixtures;

  /**
   * Path to Fitnesse root
   */
  private String m_root;
  
  /**
   * Tests results directory
   */
  private String m_execResults;

  /**
   * Resources directory
   */
   private String m_resources;
  
  private static Configurator instance = null;
 
  
  protected Configurator () {
		try {
		  DocumentBuilder xml = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			
			log.info("Configurator: start parsing config.xml");
			  
			Document doc = xml.parse(new File("config.xml"));
			loadConfigFile(doc);
		}
		catch (Exception e) {
			log.error(" Parsing config.xml error");
			e.printStackTrace();
		}
	}
	
  
  public static Configurator getInstance(){
    if(instance == null){
      instance = new Configurator();
    }
    return instance;
  }
	
	/**
	 * Private constructor
	 * We should get it like this: Configurator.getInstance();
	 * 
	 */
	public void loadConfigFile(Document doc) {
		try {
			NodeList list = doc.getDocumentElement().getChildNodes();
			
			for (int i = 0; i < list.getLength(); i++) {
				if (list.item(i).getNodeName().equals("jars")) {
					setJars(list.item(i).getAttributes().item(1).getNodeValue());
					log.info("Configurator: set JARS: "+m_jars);
				}
				if (list.item(i).getNodeName().equals("fixtures")) {
					setFixtures(list.item(i).getAttributes().item(1).getNodeValue());
					log.info("Configurator: set FIXTURES: "+m_fixtures);
				}
				if (list.item(i).getNodeName().equals("root")) {
					setRoot(list.item(i).getAttributes().item(1).getNodeValue());
					log.info("Configurator: set ROOT: "+m_root);
				}
				if (list.item(i).getNodeName().equals("execresults")) {
 				        setExecResults(list.item(i).getAttributes().item(1).getNodeValue());
     				       log.info("Configurator: set EXECRESULTS: "+m_execResults);
			        }
				if (list.item(i).getNodeName().equals("resources")) {
 				        setResources(list.item(i).getAttributes().item(1).getNodeValue());
     				        log.info("Configurator: set RESOURCES: "+m_resources);
			        }
			}
			log.info("Configurator: finish");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Get path to fixtures lib
	 * @return path to fixture lib
	 */
	public String getFixtures() {
		return m_fixtures;
	}
	
	/**
	 * Set path to fixture lib
	 * @param jars -  path to fixture lib
	 */

	public void setFixtures(String mFixtures) {
		m_fixtures = mFixtures;
	}	
	/**
	 * Get path to jars
	 * @return path to jars
	 */
	public void setJars(String jars) {
		this.m_jars = jars;
	}
	/**
	 * Set path to jars
	 * @param jars -  path to jars
	 */

	public String getJars() {
		return m_jars;
	}

	/**
	 * Get path to Fitnesse Root
	 * @return path to Fitnesse Root
	 */
	public String getRoot() {
		return m_root;
	}

	/**
	 * Set path to Fitnesse Root
	 * @param root - pat to Fitnesse Root
	 */
	public void setRoot(String root) {
		this.m_root = root;
	}
	
	/**
	 * Set tests results directory
	 * @param execResults - tests results directory
	 */
	public void setExecResults(String execResults) {
		this.m_execResults = execResults;
	}
  
	/**
	 * Get tests execution results directory
	 * @return tests results directory
	 */
	public String getExecResults() {
		return m_execResults;
	}

	/**
	 * Set resources directory
	 * @param execResults - tests results directory
	 */
	public void setResources(String resources) {
		this.m_resources = resources;
	}
  
	/**
	 * Get resources directory
	 * @return tests results directory
	 */
	public String getResources() {
		return m_resources;
	}
	 
  public String getPathDeclaration() {
    File dir = new File(getJars());
    StringBuffer result = new StringBuffer();
    
    String[] jars = dir.list(new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        return name.endsWith(JAR_FILE);
      }
    });

    result.append(TextParsingTools.PATH);
    for (String str : jars) {
      result.append(new File(dir.getAbsolutePath()
          + File.separator + str).getAbsolutePath()
          + TextParsingTools.PATH_SEPARATOR);
    }
    result.append(TextParsingTools.CR_and_NEL);
    
    return result.toString();
  }
}
